/**************************************************
* TABLE 4: Gender Gap by Staff Rank
**************************************************/

***********************
**** (A) Promotion ****
***********************
* (1) house
clear
use masterdata_house1.dta
sort core_person_id
merge core_person_id using firstcong.dta
keep if _merge == 3
drop _merge
la var firstcong "congress staffer appeared first time in the data"
keep if firstcong >=108
gen exp = 2*(congress - firstcong +1) 
replace totworkdays = ln(totworkdays +1)

gen femalestaff = 0
replace femalestaff = 1 if gender =="F"
gen memfemale = 0
replace memfemale = 1 if minwomen ==1
replace memfemale = 1 if majwomen ==1

by core_person_id member_office_id congress rank, sort: gen tt = 1 if _n == 1 
by member_office_id congress rank, sort: egen nstaffrank = sum(tt)
by member_office_id congress rank, sort: egen nstaffrank_f = sum(tt) if femalestaff == 1
sort member_office_id congress rank nstaffrank_f 
by member_office_id congress rank, sort: replace nstaffrank_f = nstaffrank_f[_n-1] if missing(nstaffrank_f) & _n >=1
replace nstaffrank_f = 0 if nstaffrank_f ==.

forvalues i = 1/7 {
by member_office_id congress, sort: egen numrank`i' = sum(tt) if rank ==`i' & femalestaff ==1
sort member_office_id congress numrank`i'
by member_office_id congress, sort: replace numrank`i' = numrank`i'[_n-1] if missing(numrank`i') & _n >=1
replace numrank`i' = 0 if numrank`i' ==.
}

forvalues i = 1/7 {
by member_office_id congress, sort: egen numrank`i'all = sum(tt) if rank ==`i' 
sort member_office_id congress numrank`i'all
by member_office_id congress, sort: replace numrank`i'all = numrank`i'all[_n-1] if missing(numrank`i'all) & _n >=1
replace numrank`i'all = 0 if numrank`i'all ==.
}

gen femalepeershare = nstaffrank_f/nstaffrank

gen boss = 0
replace boss = numrank1all if rank == 2
replace boss = numrank1all + numrank2all if rank == 3
replace boss = numrank1all + numrank2all + numrank3all if rank ==4
replace boss = numrank1all + numrank2all + numrank3all + numrank4all if rank == 5
replace boss = numrank1all + numrank2all + numrank3all + numrank4all + numrank5all if rank == 6
replace boss = numrank1all + numrank2all + numrank3all + numrank4all + numrank5all + numrank6all if rank == 7

gen bossabove = 0
replace bossabove = numrank1all if rank == 2
replace bossabove = numrank2all if rank == 3
replace bossabove = numrank3all if rank == 4
replace bossabove = numrank4all if rank == 5
replace bossabove = numrank5all if rank == 6
replace bossabove = numrank6all if rank == 7

gen femaleboss = 0
replace femaleboss = numrank1 if rank == 2
replace femaleboss = numrank1 + numrank2 if rank == 3
replace femaleboss = numrank1 + numrank2 + numrank3 if rank ==4
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 if rank == 5
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 + numrank5 if rank == 6
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 + numrank5 + numrank6 if rank == 7

gen femalebossabove = 0
replace femalebossabove = numrank1 if rank == 2
replace femalebossabove = numrank2 if rank == 3
replace femalebossabove = numrank3 if rank == 4
replace femalebossabove = numrank4 if rank == 5
replace femalebossabove = numrank5 if rank == 6
replace femalebossabove = numrank6 if rank == 7

gen femalebossratio = femaleboss/boss
replace femalebossratio = 0 if femaleboss ==.
gen femalebossaboveratio = femalebossabove/bossabove
replace femalebossaboveratio = 0 if femalebossaboveratio ==.

macro define member_char1 "dem memfemale majority chair subchr seniority maj_leader min_leader power left"
macro define member_char2 "majority chair subchr seniority maj_leader min_leader power left"
macro define staff_char1 "totworkdays nondc notfulltime exp"
macro define staff_char2 "totworkdays nondc notfulltime exp grad_degree jdphd"

sort core_person_id member_office_id congress
by core_person_id, sort: gen lagrank = rank[_n-1]
by core_person_id, sort: gen leadrank = rank[_n+1]

gen promotion = .
replace promotion = -(leadrank - rank)

gen int1 = femalestaff*dem
gen int2 = femalepeershare*femalestaff
gen int3 = femalebossratio*femalestaff

* gender gap in promotion by rank

forvalues i = 1/7 {
gen rank`i' = 0
replace rank`i' = 1 if rank ==`i'
}

forvalues i = 1/7 {
gen intrank`i' = rank`i'*femalestaff
}

macro define interaction "rank2 rank3 rank4 rank5 rank6 rank7 intrank2 intrank3 intrank4 intrank5 intrank6 intrank7"

eststo clear
eststo: quietly areg promotion $member_char2 $interaction $staff_char1 i.congress if rank >1, a(member_office_id) vce(cluster member_office_id)
eststo: quietly areg promotion $member_char2 $interaction $staff_char2 i.congress if rank >1, a(member_office_id) vce(cluster member_office_id)
esttab, star(* 0.05 ** 0.01 *** 0.001) drop(*congress*) se ar2
esttab using house_promotion_rank.tex, star(* 0.05 ** 0.01 *** 0.001) se ar2 replace

* (2) senate 
clear
use masterdata_senate1.dta
sort core_person_id
merge core_person_id using firstcong.dta
keep if _merge == 3
drop _merge
la var firstcong "congress staffer appeared first time in the data"
keep if firstcong >=108
gen exp = 2*(congress - firstcong +1) 
replace totworkdays = ln(totworkdays +1)

gen femalestaff = 0
replace femalestaff = 1 if gender =="F"
gen memfemale = 0
replace memfemale = 1 if minwomen ==1
replace memfemale = 1 if majwomen ==1

by core_person_id member_office_id congress rank, sort: gen tt = 1 if _n == 1 
by member_office_id congress rank, sort: egen nstaffrank = sum(tt)
by member_office_id congress rank, sort: egen nstaffrank_f = sum(tt) if femalestaff == 1
sort member_office_id congress rank nstaffrank_f 
by member_office_id congress rank, sort: replace nstaffrank_f = nstaffrank_f[_n-1] if missing(nstaffrank_f) & _n >=1
replace nstaffrank_f = 0 if nstaffrank_f ==.

forvalues i = 1/7 {
by member_office_id congress, sort: egen numrank`i' = sum(tt) if rank ==`i' & femalestaff ==1
sort member_office_id congress numrank`i'
by member_office_id congress, sort: replace numrank`i' = numrank`i'[_n-1] if missing(numrank`i') & _n >=1
replace numrank`i' = 0 if numrank`i' ==.
}

forvalues i = 1/7 {
by member_office_id congress, sort: egen numrank`i'all = sum(tt) if rank ==`i' 
sort member_office_id congress numrank`i'all
by member_office_id congress, sort: replace numrank`i'all = numrank`i'all[_n-1] if missing(numrank`i'all) & _n >=1
replace numrank`i'all = 0 if numrank`i'all ==.
}

gen femalepeershare = nstaffrank_f/nstaffrank

gen boss = 0
replace boss = numrank1all if rank == 2
replace boss = numrank1all + numrank2all if rank == 3
replace boss = numrank1all + numrank2all + numrank3all if rank ==4
replace boss = numrank1all + numrank2all + numrank3all + numrank4all if rank == 5
replace boss = numrank1all + numrank2all + numrank3all + numrank4all + numrank5all if rank == 6
replace boss = numrank1all + numrank2all + numrank3all + numrank4all + numrank5all + numrank6all if rank == 7

gen bossabove = 0
replace bossabove = numrank1all if rank == 2
replace bossabove = numrank2all if rank == 3
replace bossabove = numrank3all if rank == 4
replace bossabove = numrank4all if rank == 5
replace bossabove = numrank5all if rank == 6
replace bossabove = numrank6all if rank == 7

gen femaleboss = 0
replace femaleboss = numrank1 if rank == 2
replace femaleboss = numrank1 + numrank2 if rank == 3
replace femaleboss = numrank1 + numrank2 + numrank3 if rank ==4
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 if rank == 5
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 + numrank5 if rank == 6
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 + numrank5 + numrank6 if rank == 7

gen femalebossabove = 0
replace femalebossabove = numrank1 if rank == 2
replace femalebossabove = numrank2 if rank == 3
replace femalebossabove = numrank3 if rank == 4
replace femalebossabove = numrank4 if rank == 5
replace femalebossabove = numrank5 if rank == 6
replace femalebossabove = numrank6 if rank == 7

gen femalebossratio = femaleboss/boss
replace femalebossratio = 0 if femaleboss ==.
gen femalebossaboveratio = femalebossabove/bossabove
replace femalebossaboveratio = 0 if femalebossaboveratio ==.

macro define member_char1 "dem memfemale majority chair subchr seniority maj_leader min_leader power up_for_reelection freshman left"
macro define member_char2 "majority chair subchr seniority maj_leader min_leader power up_for_reelection freshman left"
macro define staff_char1 "rank totworkdays nondc notfulltime exp"
macro define staff_char2 "rank totworkdays nondc notfulltime exp grad_degree jdphd"

sort core_person_id member_office_id congress
by core_person_id, sort: gen lagrank = rank[_n-1]
by core_person_id, sort: gen leadrank = rank[_n+1]

gen promotion = .
replace promotion = -(leadrank - rank)


forvalues i = 1/7 {
gen rank`i' = 0
replace rank`i' = 1 if rank ==`i'
}

forvalues i = 1/7 {
gen intrank`i' = rank`i'*femalestaff
}

macro define interaction "rank1 rank2 rank3 rank4 rank5 rank6 rank7 intrank1 intrank2 intrank3 intrank4 intrank5 intrank6 intrank7"

eststo clear
eststo: quietly areg promotion $member_char2 $interaction $staff_char1 i.congress if rank >1, a(member_office_id) vce(cluster core_person_id)
eststo: quietly areg promotion $member_char2 $interaction $staff_char2 i.congress if rank >1, a(member_office_id) vce(cluster core_person_id)
esttab, star(* 0.05 ** 0.01 *** 0.001) drop(*congress*) se ar2
esttab using senate_promotion_rank.tex, star(* 0.05 ** 0.01 *** 0.001) se ar2 replace


**************************
**** (B) Compensation ****
**************************
* (1) house
clear
use masterdata_house1.dta
sort core_person_id
merge core_person_id using firstcong.dta
keep if _merge == 3
keep if firstcong > 107
drop _merge
la var firstcong "congress staffer appeared first time in the data"

gen femalestaff = 0
replace femalestaff = 1 if gender =="F"
gen memfemale = 0
replace memfemale = 1 if minwomen + majwomen > 0

macro define member_char1 "majority chair subchr seniority maj_leader min_leader power"
macro define staff_char1 "totworkdays nondc notfulltime exp"
macro define staff_char2 "totworkdays nondc notfulltime exp grad_degree jdphd"
gen exp = 2*(congress - firstcong +1) 

* gender pay gap by rank 

forvalues i = 1/7 {
gen rank`i' = 0
replace rank`i' = 1 if rank ==`i'
}

forvalues i = 1/7 {
gen intrank`i' = rank`i'*femalestaff
}

macro define interaction "rank1 rank2 rank3 rank4 rank5 rank6 rank7 intrank1 intrank2 intrank3 intrank4 intrank5 intrank6 intrank7"

* regression 
eststo clear
eststo: quietly areg salary $member_char1 $interaction $staff_char1 i.congress, a(member_office_id) vce(cluster member_office_id)
eststo: quietly areg salary $member_char1 $interaction $staff_char2 i.congress, a(member_office_id) vce(cluster member_office_id)
esttab, star(* 0.05 ** 0.01 *** 0.001) drop(*congress*) se ar2
esttab using house_salary_rank.tex, star(* 0.05 ** 0.01 *** 0.001) se ar2 replace

* (2) senate
clear
use masterdata_senate1.dta
sort core_person_id
merge core_person_id using firstcong.dta
keep if _merge == 3
keep if firstcong > 107
drop _merge
la var firstcong "congress staffer appeared first time in the data"

gen memfemale = 0
replace memfemale = 1 if minwomen ==1
replace memfemale = 1 if majwomen ==1
gen femalestaff = 0
replace femalestaff = 1 if gender =="F"
gen exp = 2*(congress - firstcong +1) 

macro define member_char1 "majority chair subchr seniority maj_leader min_leader power up_for_reelection freshman"
macro define staff_char1 "totworkdays nondc notfulltime exp"
macro define staff_char2 "totworkdays nondc notfulltime exp grad_degree jdphd"
encode(state), gen(state2)

* gender pay gap by rank

forvalues i = 1/7 {
gen rank`i' = 0
replace rank`i' = 1 if rank ==`i'
}

forvalues i = 1/7 {
gen intrank`i' = rank`i'*femalestaff
}

macro define interaction "rank1 rank2 rank3 rank4 rank5 rank6 rank7 intrank1 intrank2 intrank3 intrank4 intrank5 intrank6 intrank7"

* regression 
eststo clear
eststo: quietly areg salary $member_char1 $interaction $staff_char1 i.congress, a(member_office_id) vce(cluster member_office_id)
eststo: quietly areg salary $member_char1 $interaction $staff_char2 i.congress, a(member_office_id) vce(cluster member_office_id)
esttab, star(* 0.05 ** 0.01 *** 0.001) drop(*congress*) se ar2
esttab using senate_salary_rank.tex, star(* 0.05 ** 0.01 *** 0.001) se ar2 replace


